package dk.hewison.util;



import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import java.awt.*;
import java.awt.image.renderable.ParameterBlock;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-01 16:53:28 +0000 (Sun, 01 Feb 2009) $:
 * @version $Revision: 348 $:
 */
public class ThumbJAI {

    public static void makeThumbnailFile(String inputName, String outputFilename, int newWidth) throws FileNotFoundException {
        // read in the original image from an input stream

        RenderedOp image = JAI.create("fileload", inputName);
        //((OpImage) image.getRendering()).setTileCache(null);

        // now resize the image

        double scale = (double) newWidth / (double) image.getWidth();

        ParameterBlock pb = new ParameterBlock();
        pb.addSource(image); // The source image
        pb.add(scale);          // The xScale
        pb.add(scale);          // The yScale
        pb.add(0.0F);           // The x translation
        pb.add(0.0F);           // The y translation
        //pb.add(new InterpolationNearest()); // The interpolation
        //pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC_2));


        RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        RenderedOp resizedImage = JAI.create("SubsampleAverage", image, scale, scale);

        //RenderedOp resizedImage = JAI.create("scale", pb, null);

        FileOutputStream outputStream = new FileOutputStream(outputFilename);

// lastly, write the newly-resized image to an
// output stream, in a specific encoding

        JAI.create("encode", resizedImage, outputStream, "PNG", null);

    }

   

    public static void main(String[] args) {
        try {

            long start = System.currentTimeMillis();
            //String[] fileNames = {"PIA03231.JPG", "PIA03241.JPG", "PIA08814.JPG", "PIA09201.JPG", "PIA09202.JPG", "PIA09203.JPG", "PIA09205.JPG", "PIA09207.JPG", "PIA09259.JPG", "large4.png", "large3.png", "large2.png", "large.png", "marbles.tif", "xing_t24.tif"};

            //File file = new File("/picture/UK2008/");
            String a = "/Users/johnhewison/Pictures/UK2008/P1000001.JPG";
            String aout = "/Users/johnhewison/Pictures/UK2008/P1000001a.JPG";

            String b = "/Users/johnhewison/Pictures/UK2008/P1000002.JPG";
            String bout = "/Users/johnhewison/Pictures/UK2008/P1000002a.JPG";
            //3365 - 3145

            /*String c = "/Users/johnhewison/Pictures/UK2008/P1000003.JPG";
            String cout = "/Users/johnhewison/Pictures/UK2008/P1000003a.JPG";*/
            //4177

            makeThumbnailFile(a, aout, 144);
            makeThumbnailFile(b, bout, 144);
            //makeThumbnailFile(c, cout, 144);

            long stop = System.currentTimeMillis();
            Log.debug("Done. " + (stop - start));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
